<?php

namespace App\Http\Controllers\College\Merchant;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\MerchantAppAuth;
use App\Library\Rms\RmsShopApi;
use App\Models\College\CollegeTrainingOrderModel;
use App\Service\BaseService;
use App\Service\College\CollegeCourseService;
use App\Service\College\CollegeExamService;
use App\Service\College\CollegeTrainOrderService;
use Carbon\Carbon;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class TrainOrderController extends BaseController
{

    /**
     * 培训订单列表
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function list(Request $request): JsonResponse
    {
        $shopId   = $request->header('shopId', 0);
        $pageSize = $request->get('pageSize', 10);
        $pageNum  = $request->get('pageNum', 1);

        return $this->success(CollegeTrainOrderService::getTrainOrderList($pageSize, $pageNum, '', '', '', 0, '', '', 0, $shopId));
    }

    /**
     * @throws GuzzleException
     */
    public function detail(Request $request, $id): JsonResponse
    {
        return $this->success(CollegeTrainOrderService::getTrainOrderDetail($id, MerchantAppAuth::$userId));
    }

    /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function getCampusAndStaff(Request $request, $id): JsonResponse
    {
        $shopId = $request->header('shopId', 0);
        return $this->success(CollegeTrainOrderService::getCampusAndStaff($shopId, $id));
    }


    /**
     * 根据校区获得对应的培训计划
     * @param Request $request
     * @param int $id campus
     * @return JsonResponse
     */
    public function getPlanByCampus(Request $request, int $id): JsonResponse
    {
        return $this->success(CollegeTrainOrderService::getPlanByCampus($id));
    }

    /**
     * 商家确认培训信息
     * @param Request $request
     * @param $id
     * @return JsonResponse
     * @throws ValidationException
     */
    public function confirmTrainInfo(Request $request, $id): JsonResponse
    {
        $this->validate($request, [
            'campus'   => 'required|int',
            'staffIds' => 'required|array',
        ]);
        $planId = (int)$request->post('plan', 0);
        $check  = (int)$request->post('check', 0);  // 检查是否满足条件
        CollegeTrainOrderService::confirmTrainInfo($id, $request->campus, $planId, $request->staffIds, MerchantAppAuth::$userId, $check);
        return $this->success();
    }

    /**
     * 学习进度更新
     * @param Request $request
     * @param int $id 课程ID
     * @return JsonResponse
     */
    public function updateProgress(Request $request, int $id): JsonResponse
    {
        //资源ID
        $resourceId = $request->post('resourceId', 0);
        $seconds    = $request->post('seconds', 0);
        CollegeCourseService::updateProgress($id, $resourceId, $seconds, MerchantAppAuth::$userId);
        return $this->success();
    }
}
